import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
from time import sleep
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_login(self):
        self.browser.get('http://www.likeshop.com/admin/account/login.html')
        sleep(2)
        # 定位用户名输入框并输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
         # 定位密码输入框并输密码
        self.browser.find_element(By.NAME,'password').send_keys('admin')
         # 定位验证码输入框并输入验证码
        self.browser.find_element(By.NAME,'code').send_keys('111')
        # 定位登录按钮点击登录按钮
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        
    def test_shop_list(self):
        '''
        商品列表查询测试
        '''
        
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位frame子框架
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe1)
        
        # 定位商品名称输入框并输入商品名称
        self.browser.find_element(By.XPATH, '//*[@id="keyword"]').send_keys('文具')
        # 定位查询按钮并点击查询
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        
        sleep(1)
        
        # 获取列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        
        if len(tr_list) == 0:
            print('没有数据')

        is_not_bug =True
        for item in tr_list:
            shop_name = item.find_element(By.XPATH, './td[2]/div').text
            if '文具' not in shop_name:
                is_not_bug = False
        if is_not_bug:
            print('没有问题')
        else:
            print('有bug')
    

    def test_shop_add(self):
        '''
        商品新增测试
        '''
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位frame子框架
        iframe = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe)
        
        # 定位 商品发布 按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        # 定位商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        #定义当前时间
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # 定位商品名称输入框并输入数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品' + now)
        
        # 定位第一个大类的选择框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(0.5)
        # 定位第一个大类的下拉数据并点击第一个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(0.5)
        # 定位第二个类的选择框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(0.5)
        # 定位第二个类的下拉数据并点击第一个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(0.5)
        # 定位第三个类的选择框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(0.5)
        # 定位第三个类的下拉数据并点击第一个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(0.5)
        
        # 定位商品主图 图片上传按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(3)
        
        # 定位图片上传子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')

        # 进入图片上传子框架
        self.browser.switch_to.frame(upload_img_iframe_1)
        
        self.test_upload_img('D:\\Softwaie\\SoftwareTesting\\软件测试\\picture\\2.jpeg')
        
        
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 定位商品轮播图 图片上传按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)
        # 定位图片上传子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')

        # 进入图片上传子框架
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('D:\\Softwaie\\SoftwareTesting\\软件测试\\picture\\2.jpeg')
        sleep(0.5)
        
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 点击价格库存
        self.browser.find_element(By.XPATH, '/html/body/div[1]/ul/li[2]').click()
        sleep(0.5)
        
        # 填写价格、成本价、库存信息
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        
        # 回到商品子框架点击保存按钮
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        sleep(1)
        self.browser.find_element(By.LINK_TEXT, '保存').click()


    def test_upload_img(self, file_path):
        # 上传图片
        self.browser.find_element(By.XPATH, "//input[@type='file']").send_keys(file_path)
        sleep(4)
        # 选择第一张图片
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        # 点击选中图片按钮
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)

    def test_shop_sj(self):
        #先回到主框架
        self.browser.switch_to.default_content()
        #点击商品
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        # 定位frame子框架
        iframe = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe)
        
        #点击仓库中商品标签
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        #判断商品列表是否大于0
        if len(shop_list) > 0:
            #对列表第一条数据进行上架操作
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[2]').click()
            
            #点击确定按钮
            self.browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/a[1]').click()
            
        
    def test_shop_edit(self):
        '''
        商品编辑/更新测试
        '''
        
        #先回到主框架
        self.browser.switch_to.default_content()
        
        
        #点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位frame子框架
        iframe = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe)
        
        # 定位第一个商品的编辑按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
        # 定位商品编辑子框架
        shop_update_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        # 进入商品编辑子框架
        self.browser.switch_to.frame(shop_update_iframe)
        sleep(1)
        
        #定义当前时间
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # 定位商品名称输入框并输入数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').click()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('更新后的测试商品' + now)
        
        # 定位第一个大类的选择框并点击   # 定位第一个大类的下拉数据并点击第1个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(0.5)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(0.5)
        # 定位第二个类的选择框并点击     # 定位第二个类的下拉数据并点击第2个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(0.5)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
        sleep(0.5)
        # 定位第三个类的选择框并点击     # 定位第三个类的下拉数据并点击第2个
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(0.5)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[3]').click()
        sleep(0.5)
        
        #定位到商品卖点并输入
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[4]/div/input').send_keys('很实用')

        
        #鼠标悬浮到图片，定位删掉商品主图 然后点击图片上传按钮
        dt = self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[1]/img')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(1.5)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[1]/a').click()
        sleep(0.2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        
        # 定位图片上传子框架
        upload_img_iframe_3 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')

        # 进入图片上传子框架
        self.browser.switch_to.frame(upload_img_iframe_3)
        
        self.test_upload_img('D:\\Softwaie\\SoftwareTesting\\软件测试\\picture\\3.jpg')
        
        
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_update_iframe)
        
        # 定位商品轮播图 图片上传按钮并点击
        #鼠标悬浮到图片，定位删掉商品主图 然后点击图片上传按钮
        dt = self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/ul/li/img')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/ul/li/a').click()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)
        # 定位图片上传子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')

        # 进入图片上传子框架
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('D:\\Softwaie\\SoftwareTesting\\软件测试\\picture\\3.jpg')
        sleep(0.5)
        
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        # 进入商品发布子框架
        self.browser.switch_to.frame(shop_update_iframe)
        
        # 点击价格库存
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(0.5)

        # 填写价格、成本价、库存信息
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('10.00')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('52.55')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('85.66')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('1000')
       
        # 回到商品子框架点击保存按钮
        # 先回到主框架
        self.browser.switch_to.default_content()
        # 进入商品子框架
        self.browser.switch_to.frame(iframe)
        sleep(2)
        self.browser.find_element(By.LINK_TEXT, '保存').click()
        sleep(3)


    def test_logout(self):
        '''
        退出登录
        '''
        # 先回到主框架
        self.browser.switch_to.default_content()
        
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        #鼠标悬浮
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        #点击退出按钮
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()

        



if __name__=='__main__':
    likeShopTest = LikeShopTest()
    likeShopTest.test_login()
    #新增商品
    likeShopTest.test_shop_add()
    #上架商品
    likeShopTest.test_shop_sj()
    #编辑商品
    likeShopTest.test_shop_edit()
    likeShopTest.test_logout()
